/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kurvykurvy.page;

import java.awt.Point;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Vector;
import org.openide.util.Exceptions;

/**
 *
 * @author HY
 */
public class PageSpread
        implements PageSpreadLayout, Transferable
{

    public final static String MIME_TYPE = "application/kurvy-loaded-pagespread; class=" + PageSpread.class.getCanonicalName();
    public static DataFlavor PAGE_SPREAD_DATA_FLAVOR;

    static
    {
        try
        {
            PAGE_SPREAD_DATA_FLAVOR = new DataFlavor( MIME_TYPE );
        }
        catch ( ClassNotFoundException ex )
        {
            Exceptions.printStackTrace( ex );
        }
    }
    
    BufferedImage pageSpreadMask;
    Point maskOffset = new Point( 0, 0 );
    ArrayList<BufferedImage> images;
    Vector<ImageFrame> imageFrames;
    HashMap<BufferedImage, ImageFrame> imageFramesMap;
    Page[] spread;

    public PageSpread()
    {
        this.spread = new Page[ 2 ];
    }

    public void addImage( BufferedImage img )
    {
        if ( this.images == null )
            this.images = new ArrayList<BufferedImage>();

        this.images.add( img );
    }

    public void removeImage( BufferedImage img )
    {
        if ( this.images != null )
        {
            this.images.remove( img );
        }
    }

    public void clearImages()
    {
        if ( this.images != null )
        {
            this.images.clear();
        }
    }

    public void addImages( ArrayList<BufferedImage> imgs )
    {
        if ( this.images == null )
            this.images = new ArrayList<BufferedImage>();

        this.images.addAll( imgs );
    }

    public void setImages( ArrayList<BufferedImage> imgs )
    {
        this.images = imgs;
    }

    public ArrayList<BufferedImage> getImages()
    {
        return this.images;
    }

    public void attachImage( BufferedImage img, ImageFrameTemplate template )
    {
        if ( template != null && img != null )
        {
            if ( this.imageFramesMap == null )
                this.imageFramesMap = new HashMap<BufferedImage, ImageFrame>();

            ImageFrame frame = ImageFrameFactory.createImageFrame( template, img );
            this.imageFramesMap.put( img, frame );
        }
    }

    public void detachImage( BufferedImage img )
    {
        if ( this.imageFramesMap != null )
        {
            this.imageFramesMap.remove( img );
        }
    }

    public ArrayList<ImageFrame> getImageFrames()
    {
        ArrayList<ImageFrame> frames = new ArrayList<ImageFrame>();
        if ( this.imageFrames == null )
            constructImageFrames();

        frames.addAll( this.imageFrames );
        return frames;
    }

    public void setPageLayoutTemplate( PageLayoutTemplate template )
    {
        int type = LEFT_SPREAD_PAGE;
        Page newPage = null;
        if ( template.isLeft() )
        {
            System.out.println( "SETTING LEFT DECOR" );
            type = LEFT_SPREAD_PAGE;
            newPage = new PageImpl( template );
        }
        else
        {
            System.out.println( "SETTING RIGHT DECOR" );
            type = RIGHT_SPREAD_PAGE;
            Point newOffset = new Point( ((this.pageSpreadMask != null) ? this.pageSpreadMask.getWidth() / 2 : 0), 0 );
            System.out.println( "RIGHT OFFSET : " + newOffset.x + ", " + newOffset.y );
            newPage = new PageImpl( template, newOffset );
        }

        if ( newPage != null )
        {
            this.spread[type] = newPage;
            constructImageFrames();
        }
    }

    public PageLayoutTemplate getPageLayoutTemplate( int type )
    {
        return this.spread[type].getPageLayoutTemplate();
    }

    public void setPageSpreadMask( BufferedImage img )
    {
        this.pageSpreadMask = img;
    }

    public BufferedImage getPageSpreadMask()
    {
        return this.pageSpreadMask;
    }

    public Point getMaskOffset()
    {
        return this.maskOffset;
    }

    public void setMaskOffset( Point p )
    {
        this.maskOffset = p;
    }

    private void constructImageFrames()
    {
        if ( this.imageFrames == null )
            this.imageFrames = new Vector<ImageFrame>();
        else
            this.imageFrames.clear();

        for ( int i = 0; spread != null && i < spread.length; i++ )
        {
            if ( spread[i] != null )
            {
                for ( Iterator<ImageFrame> it = spread[i].getImageFrames().iterator(); it.hasNext(); )
                {
                    ImageFrame frame = it.next();
                    this.imageFrames.add( frame );
                }
            }
        }
    }

    public DataFlavor[] getTransferDataFlavors()
    {
        return new DataFlavor[]{ PAGE_SPREAD_DATA_FLAVOR };
    }

    public boolean isDataFlavorSupported( DataFlavor flavor )
    {
        boolean result = false;
        if( flavor != null && MIME_TYPE.equalsIgnoreCase( flavor.getMimeType() ) )
        {
            result = true;
        }
        return result;
    }

    public Object getTransferData( DataFlavor flavor ) throws UnsupportedFlavorException, IOException
    {
        if( isDataFlavorSupported( flavor ) )
        {
            return this;
        }
        else
        {
            return null;
        }
    }
}
